package cn.knightzz.controller;


import cn.hutool.core.util.ObjectUtil;
import cn.knightzz.entity.JsonResult;
import cn.knightzz.entity.SysUser;
import cn.knightzz.service.SysUserService;
import cn.knightzz.vo.RoleVO;
import cn.knightzz.vo.SysUserVO;
import cn.knightzz.vo.UserVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author knightzz
 * @since 2021-04-09
 */
@RestController
public class SysUserController {

    @Resource
    SysUserService sysUserService;

    /**
     * 添加用户
     *
     * @param userVO
     * @return
     */
    @PostMapping("/users")
    public JsonResult<SysUser> addUser(@RequestBody SysUserVO userVO) {

        SysUser user = new SysUser();
        // 拷贝属性
        BeanUtils.copyProperties(userVO, user);
        boolean status = sysUserService.addUser(user);
        // 添加成功
        JsonResult<SysUser> result;
        if (status) {
            result = new JsonResult<>("200", "用户添加成功!");
            result.setData(user);
        } else {
            // 添加失败
            result = new JsonResult<>("201", "用户添加失败!");
        }
        return result;
    }

    @ApiOperation("根据id查询用户信息")
    @GetMapping("/users/{id}")
    public JsonResult<SysUser> getUserById(@PathVariable Long id) {
        SysUser userById = sysUserService.getUserById(id);
        JsonResult<SysUser> result;
        if (ObjectUtil.isNull(userById)) {
            // 查询失败
            result = new JsonResult<>("201", "用户查询失败!");
        } else {
            result = new JsonResult<>("200", "用户查询成功!");
            result.setData(userById);
        }
        return result;
    }

    /**
     * 分页查询数据
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/users")
    public JsonResult<Map<String, Object>> selectUserPage(
            @RequestParam("query") String query,
            @RequestParam("pagenum") Integer pageNum,
            @RequestParam("userId") Long userId,
            @RequestParam("pagesize") Integer pageSize) {

        // 获取用户列表
        IPage<SysUser> pages = sysUserService.selectUserPage(pageSize, pageNum,userId, query);
        JsonResult<Map<String, Object>> result;
        // 获取所有数据
        List<SysUser> users = pages.getRecords();
        // 获取总记录数
        long total = pages.getTotal();
        // 获取当前页码
        long current = pages.getCurrent();
        result = new JsonResult<Map<String, Object>>("200", "分页数据获取成功!");
        HashMap<String, Object> data = new HashMap<>();
        // 封装响应数据
        data.put("total", total);
        data.put("pageNum", current);
        data.put("users", users);
        result.setData(data);
        return result;
    }

    /**
     * 更新用户状态
     *
     * @param id
     * @param enabled
     * @return
     */
    @PutMapping("/users/{id}/state/{enabled}")
    public JsonResult<Object> updateUserStatus(@PathVariable Long id, @PathVariable Integer enabled) {
        Boolean status = sysUserService.updateUserStatus(id, enabled);
        // 添加成功
        JsonResult<Object> result;
        if (status) {
            result = new JsonResult<>("200", "用户状态更新成功!");
        } else {
            // 添加失败
            result = new JsonResult<>("201", "用户状态更新失败!");
        }
        return result;
    }


    /**
     * 更新用户信息
     *
     * @param id
     * @return
     */
    @PutMapping("/users/{id}")
    public JsonResult<SysUser> updateUser(@PathVariable Long id, @RequestBody SysUser sysUser) {
        sysUser.setId(id);
        SysUser user = sysUserService.updateUser(sysUser);
        JsonResult<SysUser> result;
        if (ObjectUtil.isNull(user)) {
            // 更新失败
            result = new JsonResult<>("201", "用户更新失败!");
        } else {
            result = new JsonResult<>("200", "用户更新成功!");
            result.setData(user);
        }
        return result;
    }

    @DeleteMapping("/users/{id}")
    public JsonResult<Object> deleteUser(@PathVariable Long id) {
        boolean status = sysUserService.deleteUser(id);
        // 添加成功
        JsonResult<Object> result;
        if (status) {
            // 删除成功
            result = new JsonResult<>("200", "用户删除成功!");
        } else {
            // 添加失败
            result = new JsonResult<>("201", "用户删除失败!");
        }
        return result;
    }

    @PutMapping("/users/{id}/role")
    public JsonResult<Object> assignRole(@PathVariable Long id, @RequestBody RoleVO roleVO) {
        boolean status = sysUserService.updateUserRole(id, roleVO.getRoleId());
        // 添加成功
        JsonResult<Object> result;
        if (status) {
            // 删除成功
            result = new JsonResult<>("200", "角色分配成功!");
        } else {
            // 添加失败
            result = new JsonResult<>("201", "角色分配失败!");
        }
        return result;
    }

    @GetMapping("/users/{address}/address/{roleName}/role")
    public JsonResult<List<SysUser>> getUserByAddressAndRoleName(@PathVariable("address") String address,
                                                                 @PathVariable("roleName") String roleName){
        List<SysUser> users = sysUserService.getUserListByAddressAndRoleName(address, roleName);
        JsonResult<List<SysUser>> result = new JsonResult<>("200", "操作成功!");
        result.setData(users);
        return  result;
    }

    @GetMapping("/users/list")
    public JsonResult<List<SysUser>> getUserList(){
        List<SysUser> userList = sysUserService.getUserList();
        JsonResult<List<SysUser>> result = new JsonResult<>("200", "用户列表获取成功!");
        result.setData(userList);
        return result;
    }

    /**
     * 获取当前辖区所有管理员用户
     * @param userId
     * @return
     */
    @GetMapping("/users/admin/{id}")
    public JsonResult<List<SysUser>> getAdminUserListByUserIdOfArea(@PathVariable("id") Long userId){
        List<SysUser> userList = sysUserService.getAdminUserByArea(userId);
        JsonResult<List<SysUser>> result = new JsonResult<>("200", "用户列表获取成功!");
        result.setData(userList);
        return result;
    }

    @PutMapping("/users/pwd")
    public JsonResult<Object> editPassword(@RequestBody UserVO userVO){
        Boolean status = sysUserService.editPassword(userVO.getPassword(), userVO.getId());
        // 添加成功
        JsonResult<Object> result;
        if (status) {
            // 删除成功
            result = new JsonResult<>("200", "更新成功!");
        } else {
            // 添加失败
            result = new JsonResult<>("201", "更新失败!");
        }
        return result;
    }
}

